package ver1.util;

public class PixelCnt {
	
	private int width;
	private int height;
	private int cntX;
	private int cntY;
	
	public PixelCnt(Position ul,Position dr,int cntX,int cntY)throws Exception{
		this.cntX=cntX;
		this.cntY=cntY;
		if(ul==null||dr==null)
			throw new Exception();
		this.width=dr.getX()-ul.getX()+1;
		this.height=dr.getY()-ul.getY()+1;
		this.adjustY();
	}
	
	public int getCntX() {
		return cntX;
	}
	public void setCntX(int cntX) {
		this.cntX=cntX;
		this.adjustY();
	}
	public int getCntY() {
		return cntY;
	}
	public void setCntY(int cntY) {
		this.cntY=cntY;
		this.adjustX();
	}
	private void adjustX(){
		if(!balance()){
			this.cntX=(int)((double)width/(double)height*this.cntY);
		}
	} 
	private void adjustY(){
		if(!balance()){
			this.cntY=(int)((double)height/(double)width*this.cntX);
		}
	}
	private boolean balance(){
		return (double)width/(double)height==(double)this.cntX/(double)this.cntY;
	}
	

}
